//
//  MenuViewController.m
//  eGifr
//
//  Created by Phien Tram on 8/4/13.
//  Copyright (c) 2013 eGiftr. All rights reserved.
//

#import "MenuViewController.h"
#import "ECSlidingViewController.h"
#import "AccountSettingsViewController.h"
#import <MessageUI/MessageUI.h>
#import "AppDelegate.h"

#define ANCHOR_RIGHT_REVEAL_AMOUNT      270.0f

@interface MenuViewController () <MFMailComposeViewControllerDelegate, AccountSettingsViewDelegate>

@property (nonatomic, strong) NSArray *menuItems;
@end

@implementation MenuViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    self.menuItems = [NSArray arrayWithObjects:@"My WishList", @"Their WishList", @"Occasions Calendar", @"Scoreboard", @"Account Settings", nil];
    [self.slidingViewController setAnchorRightRevealAmount:ANCHOR_RIGHT_REVEAL_AMOUNT];
    self.slidingViewController.underLeftWidthLayout = ECFullWidth;
    [self.slidingViewController anchorTopViewTo:ECRight];
    
    UIView *footerView = [[UIView alloc] init];
    self.tableView.tableFooterView = footerView;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:NO animated:animated];
    
    [super viewWillDisappear:animated];
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectionIndex
{
    return (self.menuItems.count + 1);
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row < self.menuItems.count) {
        return 60;
    } else {
        return 100;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *cellIdentifier = @"MenuItemCell";
    static NSString *bottomIdentifier = @"BottomCellIdentifier";
    if (indexPath.row < self.menuItems.count) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            
        }
        cell.textLabel.text = [self.menuItems objectAtIndex:indexPath.row];
        
        if (indexPath.row == 0 || indexPath.row == 3) {
            cell.textLabel.textColor = [UIColor redColor];
        } else {
            cell.textLabel.textColor = [UIColor blackColor];
        }
        
        return cell;
    } else {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:bottomIdentifier];
        
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{    
    NSString *identifier = @"MyWishListNavigation";
    switch (indexPath.row) {
        case 0:
        {
            identifier = @"MyWishListNavigation";
        }
            break;
        case 1:
        {
            identifier = @"TheirWishListNavigation";
        }
            break;
        case 2:
        {
            identifier = @"CalendarNavigation";
        }
            break;
        case 3:
        {
            identifier = @"ScoreboardNavigation";
        }
            break;
        case 4:
        {
            identifier = @"AccountSettings";
        }
            break;
        default:
        {
            identifier = @"";
        }
            break;
    }
    
    if (identifier.length > 0) {
        if ([identifier isEqualToString:@"AccountSettings"]) {
            AccountSettingsViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:identifier];
            viewController.delegate = self;
            [self.navigationController pushViewController:viewController animated:YES];
        } else {
            UIViewController *newTopViewController = [self.storyboard instantiateViewControllerWithIdentifier:identifier];
            [self.slidingViewController anchorTopViewOffScreenTo:ECRight animations:nil onComplete:^{
                CGRect frame = self.slidingViewController.topViewController.view.frame;
                self.slidingViewController.topViewController = newTopViewController;
                self.slidingViewController.topViewController.view.frame = frame;
                [self.slidingViewController resetTopView];
            }];
        }
    }
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - IBAction methods

- (IBAction)sendFeedbackButtonOnPressed:(id)sender
{
    MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
    mailViewController.mailComposeDelegate = self;
    [mailViewController setToRecipients:@[@"support@egiftr.com"]];
    [mailViewController setSubject:@"Help/Feedback"];
    
    [SharedAppDelegate.window.rootViewController presentViewController:mailViewController animated:YES completion:^{
        
    }];
}

- (IBAction)findFriendsButtonOnPressed:(id)sender
{
    
}

#pragma mark - UISearchBar delegate

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [self.searchBar resignFirstResponder];
    
    //perform search here
    
}

#pragma mark - AccountSettingsViewController delegate

- (void)accountSettingsViewDidSelecteGiftrAccounts:(AccountSettingsViewController *)viewController
{
    UIViewController *newTopViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"MyWishListNavigation"];
    [self.slidingViewController anchorTopViewOffScreenTo:ECRight animations:nil onComplete:^{
        CGRect frame = self.slidingViewController.topViewController.view.frame;
        self.slidingViewController.topViewController = newTopViewController;
        self.slidingViewController.topViewController.view.frame = frame;
        [self.slidingViewController resetTopView];
    }];
}

#pragma mark - MFMailComposeViewController delegate

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    [controller dismissViewControllerAnimated:YES completion:^{
        
    }];
}

@end
